package com.yfl.tingyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yfl.tingyu.pojo.Company;
import com.yfl.tingyu.mapper.CompanyMapper;
import com.yfl.tingyu.service.ICompanyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2020-05-29
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements ICompanyService {

    @Override
    public Map<String, Object> selectCompany(Integer page, Integer rows,Map<String,Object> params) {

        //创建分页对象
        Page<Company> compage = new Page<>(page,rows);

        QueryWrapper<Company> qw=new QueryWrapper<>();
        //公司名模糊查询
        if (StringUtils.isNotBlank((String)params.get("cname"))){
            qw.like("cname",params.get("cname"));
        }
        //公司状态查询
        if (StringUtils.isNotBlank((String)params.get("status"))){
            qw.eq("status",params.get("status"));
        }

        //订单量排序
        if (StringUtils.isNotBlank((String)params.get("ordernumber"))){
            if ("desc".equals(params.get("ordernumber"))){
                qw.orderByDesc("ordernumber");
            }else {
                qw.orderByAsc("ordernumber");
            }
        }
        Page<Company> companyPage = baseMapper.selectPage(compage, qw);

        //封装前端dataGraid需要的数据
        Map<String, Object> map = new HashMap<>();

        map.put("total",companyPage.getTotal());
        map.put("rows",companyPage.getRecords());

        return map;
    }
}
